package com.hfut.eeg.common.util;

import com.hfut.eeg.common.component.EEGcache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.common.util
 * @Description: 描述EEG导联算法
 * @date 2019-12-03 08:58
 */
public class EEGLeadAlgorithm {

    private static final Logger LOGGER = LoggerFactory.getLogger(EEGLeadAlgorithm.class);
    /**
    * @Description: 单导算法
    * @param cache 脑波图数据
    * @return 单导结果
    * @author 梁琦
    * @date 2019-12-03 09:00
    * @throws null 表示脑波图数据有误
    */
    public static ArrayList<ArrayList> singleGuide(ArrayList cache){
        //对eeg数据正确性初略检验
        //判断是否为null以及eeg数据
        if(cache==null ||cache.size()<18){
            if(cache.size() == 16){
                return alllGuide(cache);
            }
            LOGGER.error("获取单导时EEGcache错误: eeg.getCachePool().size():"+cache.size());
            return null;
        }
        ArrayList<ArrayList> result = new ArrayList<>();
        ArrayList A1 = (ArrayList) cache.get(16),A2 = (ArrayList) cache.get(17);
        if(A1.size() != A2.size()){
            LOGGER.error("A1与A2数据大小不匹配：A1:"+A1.size()+" A2: "+A2.size());
            return null;
        }
        for (int i=0;i<16;i++){
            ArrayList tmp = new ArrayList(),each = (ArrayList) cache.get(i);
            if(each.size() != A1.size()){
                LOGGER.error("通道数据大小与A1不匹配：通道"+"通道大小："+each.size()+" A1大小："+A1.size());
            }
            for (int index =0;index<A1.size();index++){
                ArrayList A= index%2==0?A1:A2;
                //强制转换为float
                float num = (Float) each.get(index)-(Float) A.get(index);
                tmp.add(num);
            }
            result.add(tmp);
        }
        return result;
    }


    /**
    * @Description: 均导算法
    * @param eeg eeg数据
    * @return 均导结果
    * @author 梁琦
    * @date 2019-12-30 19:55
    * @throws
    */
    public static ArrayList<ArrayList> alllGuide(ArrayList eeg){
        //只需要计算16个脑波图的数据
        if(eeg==null || eeg.size()<16){
            LOGGER.error("获取均导时eeg错误: eeg.size():"+eeg.size());
            return null;
        }
        ArrayList<ArrayList> result = new ArrayList<>();
        ArrayList theta = getTheta(eeg);
        for (Object o : eeg) {
            ArrayList tmp = (ArrayList) o,
                    list = new ArrayList<>();
            for (int index = 0; index < tmp.size(); index++) {
                list.add((Float) tmp.get(index) - (Float) theta.get(index));
            }
            result.add(list);
        }
        return result;
    }

    /**
    * @Description: 脑部能量图计算
    * @param list 脑部能量数组
    * @return 脑部能量结果
    * @author 梁琦
    * @date 2019-12-30 19:56
    * @throws
    */
    private static ArrayList<Float> getTheta(ArrayList list){
        ArrayList result =new ArrayList();
        ArrayList tmp = (ArrayList) list.get(0);
        for (int index =0;index<tmp.size();index++) {
            float sum =0;
            for (Object o : list) {
                ArrayList each = (ArrayList) o;
                sum += (Float) each.get(index);
            }
            result.add(sum/16);
        }
        return result;
    }

}
